package fr.upmc.i3s.upmcontact.views.dialogs;

import java.util.Locale;

import fr.upmc.i3s.upmcontact.R;
import fr.upmc.i3s.upmcontact.contents.MerchantInfo;
import fr.upmc.i3s.upmcontact.tools.BitmapFactoryAdapter;
import fr.upmc.i3s.upmcontact.tools.DateUtils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("DefaultLocale")
public class MerchantInfoDetailsDialog extends Activity {
	
	private static final String TAG = "MerchantInfoDetailsDialog";
	
	public static String PARAM_INFO = "MerchantInfoDetailsDialog.object";
	
	private MerchantInfo mi;
	private int activityWidth;

	private TextView tvTitle;
	private TextView tvMerchantName;
	private TextView tvAddress;
	private ImageView ivPhoto;
	private TextView tvDescription;
	private TextView tvDates;
	private TextView tvRemaining;
	private TextView tvConditions;
	private TextView tvLink;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_merchant_info);
        
        // set dialog size (90% screen width) :
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        this.activityWidth = (int) (metrics.widthPixels * 0.90);
        getWindow().setLayout(activityWidth, LayoutParams.WRAP_CONTENT);
        
        // get extras parameters 
        this.mi = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	this.mi = (MerchantInfo) extras.getSerializable(PARAM_INFO);
        }
        Log.d(TAG, "extras.UpmcInfo = "+mi);
        
        fetchViews();
        customizeViews();
        setListeners();
	}
	
	private void fetchViews() {
		this.tvTitle = (TextView) this.findViewById(R.id.textView_dialog_title);
		this.tvMerchantName = (TextView) this.findViewById(R.id.textView_dialog_merchant_name);
		this.tvAddress = (TextView) this.findViewById(R.id.textView_dialog_address);
		this.ivPhoto = (ImageView) this.findViewById(R.id.imageView_dialog_photo);
		this.tvDescription = (TextView) this.findViewById(R.id.textView_dialog_description);
		this.tvDates = (TextView) this.findViewById(R.id.textView_dialog_dates);
		this.tvRemaining = (TextView) this.findViewById(R.id.textView_dialog_remaining);
		this.tvConditions = (TextView) this.findViewById(R.id.textView_dialog_conditions);
		this.tvLink = (TextView) this.findViewById(R.id.textView_dialog_link);
	}
	
	private void customizeViews() {
		if(mi != null) {
			this.tvTitle.setText(mi.getTitle());
			this.tvMerchantName.setText(mi.getPublishedBy().getUsername().toUpperCase(Locale.getDefault()));
			this.tvAddress.setText(mi.getMerchantAddress());
	 		if(mi.getPhotoPath() != null) { 
	 			Bitmap bm = BitmapFactoryAdapter.decodeFile(mi.getPhotoPath(), 400);
	 			ivPhoto.setImageBitmap(bm);
	 		}
	 		this.ivPhoto.setAdjustViewBounds(true);
			this.tvDescription.setText(mi.getDescription()); 
			this.tvDates.setText(getString(R.string.display_begin_date)+" "+
											DateUtils.toDisplayDate(mi.getBeginDate())+" - "+
											DateUtils.toDisplayTime(mi.getBeginDate())+"\n"+
								 getString(R.string.display_end_date)+" "+
											DateUtils.toDisplayDate(mi.getEndDate())+" - "+
											DateUtils.toDisplayTime(mi.getEndDate()));
	 		switch(mi.getPromotionStatus()) {
	 		case MerchantInfo.STATE_SHORTLY_AVAILABLE:
	 			tvRemaining.setTextColor(Color.BLUE);
	 			tvRemaining.setText(R.string.display_shorly_available);
	 			break;
	 		case MerchantInfo.STATE_ONGOING:
	 			tvRemaining.setTextColor(Color.GREEN);
				tvRemaining.setText(DateUtils.getDaysNumber(DateUtils.today(), mi.getEndDate())+" "+getString(R.string.display_remaining_days));
	 			break;
	 		case MerchantInfo.STATE_ENDED:
	 			tvRemaining.setTextColor(Color.RED);
	 			tvRemaining.setText(R.string.display_ended);
	 			break;
	 		}
			this.tvConditions.setText(mi.getConditions());
			this.tvLink.setText(mi.getLink());
		}
	}
	
	private void setListeners() {
		
	}
	
}
